// components/login/login.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    modalShow: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onGotUserInfo(event) {
      console.log(event)
      const userInfo = event.detail.userInfo
      wx.login({
        success: (res) => {
          console.log(res)
          wx.request({
            url: 'http://127.0.0.1:8000/wx_login/',
            method: 'POST',
            data: {
              code: res.code,
              nickname: userInfo.nickName,
              avatar_url: userInfo.avatarUrl,
              gender: userInfo.gender
            },
            //登录成功后返回token保存在storage中
            success: res => {
              console.log(res)
              //token存入storage
              wx.setStorageSync('jwt_token', res.data.token)
              wx.setStorageSync('user_id', res.data.user_id)
              wx.setStorageSync('userInfo', userInfo)
              if (res.data.user_id) {
                this.setData({
                  modalShow: false,
                })
                this.triggerEvent('loginsuccess', userInfo)
              } else {
                this.triggerEvent('loginfail')
              }
            }
          })
        }
      })
      // if (userInfo) {
      //   this.setData({
      //     modalShow: false,
      //   })
      //   this.triggerEvent('loginsuccess', userInfo)
      // } else {
      //   this.triggerEvent('loginfail')
      // }
    }
  }
})